﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using PAT.Entities;
using PAT.Data;

#endregion

namespace PAT.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="PatRolProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class PatRolProviderBaseCore : EntityProviderBase<PAT.Entities.PatRol, PAT.Entities.PatRolKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByIdMenuFromPatRolMenu
		
		/// <summary>
		///		Gets PAT_ROL objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="_idMenu"></param>
		/// <returns>Returns a typed collection of PatRol objects.</returns>
		public TList<PatRol> GetByIdMenuFromPatRolMenu(System.Int16 _idMenu)
		{
			int count = -1;
			return GetByIdMenuFromPatRolMenu(null,_idMenu, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets PAT.Entities.PatRol objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idMenu"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatRol objects.</returns>
		public TList<PatRol> GetByIdMenuFromPatRolMenu(System.Int16 _idMenu, int start, int pageLength)
		{
			int count = -1;
			return GetByIdMenuFromPatRolMenu(null, _idMenu, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdMenuFromPatRolMenu(TransactionManager transactionManager, System.Int16 _idMenu)
		{
			int count = -1;
			return GetByIdMenuFromPatRolMenu(transactionManager, _idMenu, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdMenuFromPatRolMenu(TransactionManager transactionManager, System.Int16 _idMenu,int start, int pageLength)
		{
			int count = -1;
			return GetByIdMenuFromPatRolMenu(transactionManager, _idMenu, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="_idMenu"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdMenuFromPatRolMenu(System.Int16 _idMenu,int start, int pageLength, out int count)
		{
			
			return GetByIdMenuFromPatRolMenu(null, _idMenu, start, pageLength, out count);
		}


		/// <summary>
		///		Gets PAT_ROL objects from the datasource by ID_MENU in the
		///		PAT_ROL_MENU table. Table PAT_ROL is related to table PAT_MENU
		///		through the (M:N) relationship defined in the PAT_ROL_MENU table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idMenu"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatRol objects.</returns>
		public abstract TList<PatRol> GetByIdMenuFromPatRolMenu(TransactionManager transactionManager,System.Int16 _idMenu, int start, int pageLength, out int count);
		
		#endregion GetByIdMenuFromPatRolMenu
		
		#region GetByIdUsuarioFromPatUsuarioRol
		
		/// <summary>
		///		Gets PAT_ROL objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <returns>Returns a typed collection of PatRol objects.</returns>
		public TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(System.Int32 _idUsuario)
		{
			int count = -1;
			return GetByIdUsuarioFromPatUsuarioRol(null,_idUsuario, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets PAT.Entities.PatRol objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatRol objects.</returns>
		public TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(System.Int32 _idUsuario, int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuarioFromPatUsuarioRol(null, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(TransactionManager transactionManager, System.Int32 _idUsuario)
		{
			int count = -1;
			return GetByIdUsuarioFromPatUsuarioRol(transactionManager, _idUsuario, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(TransactionManager transactionManager, System.Int32 _idUsuario,int start, int pageLength)
		{
			int count = -1;
			return GetByIdUsuarioFromPatUsuarioRol(transactionManager, _idUsuario, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets PatRol objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="_idUsuario"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of PAT_ROL objects.</returns>
		public TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(System.Int32 _idUsuario,int start, int pageLength, out int count)
		{
			
			return GetByIdUsuarioFromPatUsuarioRol(null, _idUsuario, start, pageLength, out count);
		}


		/// <summary>
		///		Gets PAT_ROL objects from the datasource by ID_USUARIO in the
		///		PAT_USUARIO_ROL table. Table PAT_ROL is related to table PAT_USUARIO
		///		through the (M:N) relationship defined in the PAT_USUARIO_ROL table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_idUsuario"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of PatRol objects.</returns>
		public abstract TList<PatRol> GetByIdUsuarioFromPatUsuarioRol(TransactionManager transactionManager,System.Int32 _idUsuario, int start, int pageLength, out int count);
		
		#endregion GetByIdUsuarioFromPatUsuarioRol
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, PAT.Entities.PatRolKey key)
		{
			return Delete(transactionManager, key.IdRol);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_idRol">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int16 _idRol)
		{
			return Delete(null, _idRol);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int16 _idRol);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override PAT.Entities.PatRol Get(TransactionManager transactionManager, PAT.Entities.PatRolKey key, int start, int pageLength)
		{
			return GetByIdRol(transactionManager, key.IdRol, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_PAT_ROL index.
		/// </summary>
		/// <param name="_idRol"></param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public PAT.Entities.PatRol GetByIdRol(System.Int16 _idRol)
		{
			int count = -1;
			return GetByIdRol(null,_idRol, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_ROL index.
		/// </summary>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public PAT.Entities.PatRol GetByIdRol(System.Int16 _idRol, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRol(null, _idRol, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_ROL index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public PAT.Entities.PatRol GetByIdRol(TransactionManager transactionManager, System.Int16 _idRol)
		{
			int count = -1;
			return GetByIdRol(transactionManager, _idRol, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_ROL index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public PAT.Entities.PatRol GetByIdRol(TransactionManager transactionManager, System.Int16 _idRol, int start, int pageLength)
		{
			int count = -1;
			return GetByIdRol(transactionManager, _idRol, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_ROL index.
		/// </summary>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public PAT.Entities.PatRol GetByIdRol(System.Int16 _idRol, int start, int pageLength, out int count)
		{
			return GetByIdRol(null, _idRol, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_PAT_ROL index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_idRol"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="PAT.Entities.PatRol"/> class.</returns>
		public abstract PAT.Entities.PatRol GetByIdRol(TransactionManager transactionManager, System.Int16 _idRol, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;PatRol&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;PatRol&gt;"/></returns>
		public static TList<PatRol> Fill(IDataReader reader, TList<PatRol> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				PAT.Entities.PatRol c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("PatRol")
					.Append("|").Append((System.Int16)reader[((int)PatRolColumn.IdRol - 1)]).ToString();
					c = EntityManager.LocateOrCreate<PatRol>(
					key.ToString(), // EntityTrackingKey
					"PatRol",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new PAT.Entities.PatRol();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.IdRol = (System.Int16)reader[((int)PatRolColumn.IdRol - 1)];
					c.DescripcionRol = (System.String)reader[((int)PatRolColumn.DescripcionRol - 1)];
					c.Activo = (System.Boolean)reader[((int)PatRolColumn.Activo - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatRol"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatRol"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, PAT.Entities.PatRol entity)
		{
			if (!reader.Read()) return;
			
			entity.IdRol = (System.Int16)reader[((int)PatRolColumn.IdRol - 1)];
			entity.DescripcionRol = (System.String)reader[((int)PatRolColumn.DescripcionRol - 1)];
			entity.Activo = (System.Boolean)reader[((int)PatRolColumn.Activo - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="PAT.Entities.PatRol"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatRol"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, PAT.Entities.PatRol entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.IdRol = (System.Int16)dataRow["ID_ROL"];
			entity.DescripcionRol = (System.String)dataRow["DESCRIPCION_ROL"];
			entity.Activo = (System.Boolean)dataRow["ACTIVO"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="PAT.Entities.PatRol"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">PAT.Entities.PatRol Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, PAT.Entities.PatRol entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByIdRol methods when available
			
			#region IdUsuarioPatUsuarioCollection_From_PatUsuarioRol
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<PatUsuario>|IdUsuarioPatUsuarioCollection_From_PatUsuarioRol", deepLoadType, innerList))
			{
				entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol = DataRepository.PatUsuarioProvider.GetByIdRolFromPatUsuarioRol(transactionManager, entity.IdRol);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdUsuarioPatUsuarioCollection_From_PatUsuarioRol' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol != null)
				{
					deepHandles.Add("IdUsuarioPatUsuarioCollection_From_PatUsuarioRol",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< PatUsuario >) DataRepository.PatUsuarioProvider.DeepLoad,
						new object[] { transactionManager, entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region PatUsuarioRolCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatUsuarioRol>|PatUsuarioRolCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatUsuarioRolCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatUsuarioRolCollection = DataRepository.PatUsuarioRolProvider.GetByIdRol(transactionManager, entity.IdRol);

				if (deep && entity.PatUsuarioRolCollection.Count > 0)
				{
					deepHandles.Add("PatUsuarioRolCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatUsuarioRol>) DataRepository.PatUsuarioRolProvider.DeepLoad,
						new object[] { transactionManager, entity.PatUsuarioRolCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region PatRolMenuCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<PatRolMenu>|PatRolMenuCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'PatRolMenuCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.PatRolMenuCollection = DataRepository.PatRolMenuProvider.GetByIdRol(transactionManager, entity.IdRol);

				if (deep && entity.PatRolMenuCollection.Count > 0)
				{
					deepHandles.Add("PatRolMenuCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<PatRolMenu>) DataRepository.PatRolMenuProvider.DeepLoad,
						new object[] { transactionManager, entity.PatRolMenuCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region IdMenuPatMenuCollection_From_PatRolMenu
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<PatMenu>|IdMenuPatMenuCollection_From_PatRolMenu", deepLoadType, innerList))
			{
				entity.IdMenuPatMenuCollection_From_PatRolMenu = DataRepository.PatMenuProvider.GetByIdRolFromPatRolMenu(transactionManager, entity.IdRol);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'IdMenuPatMenuCollection_From_PatRolMenu' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.IdMenuPatMenuCollection_From_PatRolMenu != null)
				{
					deepHandles.Add("IdMenuPatMenuCollection_From_PatRolMenu",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< PatMenu >) DataRepository.PatMenuProvider.DeepLoad,
						new object[] { transactionManager, entity.IdMenuPatMenuCollection_From_PatRolMenu, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the PAT.Entities.PatRol object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">PAT.Entities.PatRol instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">PAT.Entities.PatRol Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, PAT.Entities.PatRol entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region IdUsuarioPatUsuarioCollection_From_PatUsuarioRol>
			if (CanDeepSave(entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol, "List<PatUsuario>|IdUsuarioPatUsuarioCollection_From_PatUsuarioRol", deepSaveType, innerList))
			{
				if (entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol.Count > 0 || entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol.DeletedItems.Count > 0)
				{
					DataRepository.PatUsuarioProvider.Save(transactionManager, entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol); 
					deepHandles.Add("IdUsuarioPatUsuarioCollection_From_PatUsuarioRol",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<PatUsuario>) DataRepository.PatUsuarioProvider.DeepSave,
						new object[] { transactionManager, entity.IdUsuarioPatUsuarioCollection_From_PatUsuarioRol, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 

			#region IdMenuPatMenuCollection_From_PatRolMenu>
			if (CanDeepSave(entity.IdMenuPatMenuCollection_From_PatRolMenu, "List<PatMenu>|IdMenuPatMenuCollection_From_PatRolMenu", deepSaveType, innerList))
			{
				if (entity.IdMenuPatMenuCollection_From_PatRolMenu.Count > 0 || entity.IdMenuPatMenuCollection_From_PatRolMenu.DeletedItems.Count > 0)
				{
					DataRepository.PatMenuProvider.Save(transactionManager, entity.IdMenuPatMenuCollection_From_PatRolMenu); 
					deepHandles.Add("IdMenuPatMenuCollection_From_PatRolMenu",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<PatMenu>) DataRepository.PatMenuProvider.DeepSave,
						new object[] { transactionManager, entity.IdMenuPatMenuCollection_From_PatRolMenu, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<PatUsuarioRol>
				if (CanDeepSave(entity.PatUsuarioRolCollection, "List<PatUsuarioRol>|PatUsuarioRolCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatUsuarioRol child in entity.PatUsuarioRolCollection)
					{
						if(child.IdRolSource != null)
						{
								child.IdRol = child.IdRolSource.IdRol;
						}

						if(child.IdUsuarioSource != null)
						{
								child.IdUsuario = child.IdUsuarioSource.IdUsuario;
						}

					}

					if (entity.PatUsuarioRolCollection.Count > 0 || entity.PatUsuarioRolCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatUsuarioRolProvider.Save(transactionManager, entity.PatUsuarioRolCollection);
						
						deepHandles.Add("PatUsuarioRolCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatUsuarioRol >) DataRepository.PatUsuarioRolProvider.DeepSave,
							new object[] { transactionManager, entity.PatUsuarioRolCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<PatRolMenu>
				if (CanDeepSave(entity.PatRolMenuCollection, "List<PatRolMenu>|PatRolMenuCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(PatRolMenu child in entity.PatRolMenuCollection)
					{
						if(child.IdRolSource != null)
						{
								child.IdRol = child.IdRolSource.IdRol;
						}

						if(child.IdMenuSource != null)
						{
								child.IdMenu = child.IdMenuSource.IdMenu;
						}

					}

					if (entity.PatRolMenuCollection.Count > 0 || entity.PatRolMenuCollection.DeletedItems.Count > 0)
					{
						//DataRepository.PatRolMenuProvider.Save(transactionManager, entity.PatRolMenuCollection);
						
						deepHandles.Add("PatRolMenuCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< PatRolMenu >) DataRepository.PatRolMenuProvider.DeepSave,
							new object[] { transactionManager, entity.PatRolMenuCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region PatRolChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>PAT.Entities.PatRol</c>
	///</summary>
	public enum PatRolChildEntityTypes
	{

		///<summary>
		/// Collection of <c>PatRol</c> as ManyToMany for PatUsuarioCollection_From_PatUsuarioRol
		///</summary>
		[ChildEntityType(typeof(TList<PatUsuario>))]
		IdUsuarioPatUsuarioCollection_From_PatUsuarioRol,

		///<summary>
		/// Collection of <c>PatRol</c> as OneToMany for PatUsuarioRolCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatUsuarioRol>))]
		PatUsuarioRolCollection,

		///<summary>
		/// Collection of <c>PatRol</c> as OneToMany for PatRolMenuCollection
		///</summary>
		[ChildEntityType(typeof(TList<PatRolMenu>))]
		PatRolMenuCollection,

		///<summary>
		/// Collection of <c>PatRol</c> as ManyToMany for PatMenuCollection_From_PatRolMenu
		///</summary>
		[ChildEntityType(typeof(TList<PatMenu>))]
		IdMenuPatMenuCollection_From_PatRolMenu,
	}
	
	#endregion PatRolChildEntityTypes
	
	#region PatRolFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;PatRolColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatRol"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatRolFilterBuilder : SqlFilterBuilder<PatRolColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatRolFilterBuilder class.
		/// </summary>
		public PatRolFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatRolFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatRolFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatRolFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatRolFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatRolFilterBuilder
	
	#region PatRolParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;PatRolColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatRol"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatRolParameterBuilder : ParameterizedSqlFilterBuilder<PatRolColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatRolParameterBuilder class.
		/// </summary>
		public PatRolParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the PatRolParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public PatRolParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the PatRolParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public PatRolParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion PatRolParameterBuilder
	
	#region PatRolSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;PatRolColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatRol"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class PatRolSortBuilder : SqlSortBuilder<PatRolColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatRolSqlSortBuilder class.
		/// </summary>
		public PatRolSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion PatRolSortBuilder
	
} // end namespace
